package com.maya.controller;

import javax.servlet.http.HttpSession;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.maya.model.User;
import com.maya.service.UserService;


@Controller
public class UnFollowUserController {
	

	
	@Autowired
	private UserService userService;
	
	@ResponseBody
	@RequestMapping("/unfollowuser")
    public String unfollowuser(String viewedid,HttpSession session){
       
		User user = (User)session.getAttribute("currUser");
        User viewedUser = userService.get(new ObjectId(viewedid));

        if(null == user) {
            
            return "error";
        }
        System.out.println(user.getName() + "取消关注" + viewedUser.getName());

        userService.unFollowPerson(user,viewedUser);
        
        user = userService.get(user.getId());
        System.out.println("====返回成功 长度变为 " + user.getFollowingIds().size() );
       
        session.setAttribute("currUser", user);
        return "OK";
    }
}

